/*
  Created by 1271107612@qq.com.
  User: DELL
  Date: 2022/4/9
  Time: 12:56
  DESCRIPTION: 
*/
package com.zzy.face.utils.jwt;

import io.jsonwebtoken.Claims;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JwtInterceptor implements HandlerInterceptor {
    private final Jwt jwt;
    private final int identity;
    public JwtInterceptor(Jwt jwt,int identity) {
        this.jwt = jwt;
        this.identity = identity;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        Claims token = jwt.token(request);

        if (token == null) {
            throw new RuntimeException("无 token ，请重新登陆");
        }
        System.out.println(identity+"  "+Integer.parseInt(token.get("identity").toString()));
        if (Integer.parseInt(token.get("identity").toString())<(identity)) {
            throw new RuntimeException("请重新登陆");
        }
        return true;
    }
}


